home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / cubezip.exe / CUBESRCH.INC < prev    next >
Text File  |  1991-10-13  |  6KB  |  147 lines

  1. ;┌──────────────────────────────────────────────────────────────────────────┐
  2. ;│             CUBESRCH.INC - Include file for the Cube Solver            │
  3. ;└──────────────────────────────────────────────────────────────────────────┘
  4. ;┌──────────────────────────────────────────────────────────────────────────┐
  5. ;│    CurrentTreePosition is a word-size table, indexed by piece number        │
  6. ;│    that contains the branch number we're currently exploring. It's        │
  7. ;│    initialized to 0's and each branch is explored from there...        │
  8. ;└──────────────────────────────────────────────────────────────────────────┘
  9. CurrentTreePosition    dw    NUMBER_OF_PIECES dup (0)
  10.  
  11. ;┌──────────────────────────────────────────────────────────────────────────┐
  12. ;│    CurrentMasterCube is the master puzzle cube as it stands at each        │
  13. ;│    level of the tree.                            │
  14. ;└──────────────────────────────────────────────────────────────────────────┘
  15. MasterCubeLevel        db   ((SUB_CUBE_COUNT+1) * NUMBER_OF_PIECES) dup(0)
  16.  
  17. MergedPieceOffset    dw    ?    ; offset of the piece to be merged
  18.  
  19. ;┌──────────────────────────────────────────────────────────────────────────┐
  20. ;│  This table of SUB_CUBE_COUNT displacements is used to avoid multiplies  │
  21. ;└──────────────────────────────────────────────────────────────────────────┘
  22. PDT        LABEL    WORD    ; PDT = Piece Displacement Table
  23. Displacement    =    0
  24.         REPT    128
  25.         dw    Displacement
  26. Displacement    =    Displacement + (SUB_CUBE_COUNT+1)
  27.         ENDM
  28.  
  29. ;****************************************************************************
  30. ;****************************************************************************
  31. ;┌──────────────────────────────────────────────────────────────────────────┐
  32. ;│                       The tree searching code ...                │
  33. ;└──────────────────────────────────────────────────────────────────────────┘
  34.  
  35. ;────────────────────────────────────────────────────────────────────────────
  36. ;  We come here with CurrentPiece = 0-8 indicating the piece we're trying
  37. ;  to find a place to fit into the cube ...
  38. ;
  39. ;  Register Usage:
  40. ;       ax - general
  41. ;    bx - CurrentPiece * 2
  42. ;    cx - counter
  43. ;    dx - count of cubes/2 (reloads cx)
  44. ;    si - compare pointer #1
  45. ;    di - compare pointer #2
  46. ;    bp - working table offset in DS
  47. ;
  48. ;────────────────────────────────────────────────────────────────────────────
  49. SearchTheTree:    zero    bx                ; initial piece #
  50.         mov    dx, (SUB_CUBE_COUNT+1)/2    ; get byte count
  51.         mov    bp, OFFSET WorkingPieceTable    ; and destination
  52. ;────────────────────────────────────────────────────────────────────────────
  53. PlaceTheFirst:    zero    ax            ; clear out the working cube
  54.         mov    cx, dx
  55.         mov    di, bp            ; working piece table
  56.  
  57.         rep stosw
  58. ;────────────────────────────────────────────────────────────────────────────
  59. TestThePiece:    mov    cx, dx            ; get the count
  60.         mov    si, bp            ; and the Working Table
  61.         call    GetPieceOffset        ; di gets piece's image
  62.         push    di
  63.  
  64. ;────────────────────────────────────────────────────────────────────────────
  65. CheckCollision:    lodsw            ; get piece image, inc si
  66.         test    ax, [di]    ; does it intersect the working?
  67.         jnz    Collision    ; yes, so terminate it
  68.         add    di, 2        ; point to next working
  69.         loop    CheckCollision
  70.  
  71. ;────────────────────────────────────────────────────────────────────────────
  72.         mov    cx, dx
  73.         pop    si        ; recover the piece's image
  74.         mov    di, bp        ; and the current working table
  75. MergePieceIn:    lodsw            ; get piece image and point to next
  76.         or    [di], ax    ; merge the image's bits
  77.         add    di, 2        ; point to next working
  78.         loop    MergePieceIn
  79.  
  80. ;────────────────────────────────────────────────────────────────────────────
  81. ShowThePiece:    call    ShowCurrentPiece     ; preserves bx, dx, bp
  82.         call    OperatorCheck
  83. ;────────────────────────────────────────────────────────────────────────────
  84.         mov    cx, dx
  85.         mov    si, bp            ; working piece table
  86.         mov    di, PDT[bx]        ; get the level to reload
  87.         add    di, OFFSET MasterCubeLevel ; reset to level's master
  88.         rep movsw        
  89.  
  90.         cmp    bx, 8*2        ; are we already at the top?
  91.         je    Collision2    ; yep, so look for another match!
  92.         add    bx, 2        ; advance to next level
  93.         jmp    TestThePiece
  94.  
  95. ;────────────────────────────────────────────────────────────────────────────
  96. Collision:    add    sp, 2        ; discard the saved di
  97. Collision2:    call    NextBranch    ; advance this level's branch
  98.         jb    TestThePiece    ; got another branch to try
  99.                     ; no more branches, so back up!
  100. ;────────────────────────────────────────────────────────────────────────────
  101. BackUpTheTree:    call    BlankCurrentPiece
  102.         sub    bx, 2        ; point to prior piece (level*2)
  103.         jnz    GetNextBranch
  104.         cmp    CurrentTreePosition[bx], 2
  105.         jae    Pause
  106.  
  107. ;────────────────────────────────────────────────────────────────────────────
  108. GetNextBranch:    call    NextBranch
  109.         jae    BackUpTheTree
  110.  
  111. ;────────────────────────────────────────────────────────────────────────────
  112. SetPriorLevel:    mov    cx, dx
  113.         check    bx            ; are we back to level 0 ?
  114.         jz    PlaceTheFirst        ; yep ... so clear us out
  115.  
  116.  
  117. ;────────────────────────────────────────────────────────────────────────────
  118. CopyPriorImage:    mov    si, PDT[bx-2]        ; get the level to reload
  119.         add    si, OFFSET MasterCubeLevel ; reset to level's master
  120.         mov    di, bp            ; working piece table
  121.         rep movsw        
  122.         jmp    TestThePiece
  123.  
  124.  
  125. ;╒══════════════════════════════════════════════════════════════════════════╕
  126. ;│       Advances the branch at position BX, or zeros it if no more        │
  127. ;└──────────────────────────────────────────────────────────────────────────┘
  128. NextBranch:    mov    ax, CurrentTreePosition[bx]    ; setup for next pos
  129.         inc    ax
  130.         cmp    ax, PieceImageCounts[bx]    ; are we exhausted?
  131.         jb    SaveNewBranch
  132.         zero    ax
  133. SaveNewBranch:    mov    CurrentTreePosition[bx], ax
  134.         ret
  135.  
  136.  
  137. ;╒══════════════════════════════════════════════════════════════════════════╕
  138. ;│       Sets DI to the offset of the piece just merged ...            │
  139. ;└──────────────────────────────────────────────────────────────────────────┘
  140. GetPieceOffset:    mov    di, CurrentTreePosition[bx]    ; get current pos
  141.         double    di                ; PDT is word indexed
  142.         mov    di, PDT[di]            ; get offset
  143.         add    di, PieceTableOffsets[bx]    ; and start of table
  144.         mov    MergedPieceOffset, di
  145.         ret
  146.  
  147.